﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.Common;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.UDT
{
    [Access("262002,262003")]
    public partial class Manage : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
            }
        }

        #region 网页属性

        /// <summary>
        ///     是否处于搜索状态
        /// </summary>
        private bool IsSearch
        {
            get
            {
                if (ViewState["IsSearch"] != null)
                {
                    return Convert.ToBoolean(ViewState["IsSearch"]);
                }
                return false;
            }
            set { ViewState["IsSearch"] = value; }
        }

        #endregion

        public void BindList(int pageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            this.BC_Path.Items.Add(new ListItem {Text = "标签管理", Value = "UDT_Manage.aspx"});

            if (IsSearch)
            {
                this.BC_Path.Items.Add(new ListItem
                {
                    Text = string.Format("搜索“{0}”", this.txtKeyWord.Text.Trim()),
                    Value = ""
                });
            }
            var pp = new PagingParam
            {
                PageSize = this.PageNavigation1.PageSize,
                PageNo = pageNo
            };


            this.GV_List.DataSource = new UDTManage().BindList(this.ddlSort.SelectedValue, this.txtKeyWord.Text, pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }

        protected void btnDelSeletedID_Click(object sender, EventArgs e)
        {
            new UDTManage().Delete(GetSelectedRow.GetIDs(this.GV_List), this.UpdatePanel1);

            BindList(1);
        }

        protected void btnStop_Click(object sender, EventArgs e)
        {
            var res = new UDTManage().StopitOrRunit(GetSelectedRow.GetIDs(this.GV_List));
            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                BindList(this.PageNavigation1.PageNo);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }

        protected void btnUnStop_Click(object sender, EventArgs e)
        {
            var res = new UDTManage().StopitOrRunit(GetSelectedRow.GetIDs(this.GV_List));

            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                BindList(this.PageNavigation1.PageNo);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var tc = e.Row.Cells[3];

                switch (tc.Text)
                {
                    case "0":
                        tc.Text = "SqlServer版主数据库";
                        break;
                    case "1":
                        tc.Text = "Access版主数据库";
                        break;
                    case "2":
                        tc.Text = "外部SqlServer";
                        break;
                    case "3":
                        tc.Text = "外部Access";
                        break;
                }
                tc = e.Row.Cells[4];

                switch (tc.Text)
                {
                    case "0":
                        tc.Text = "普通";
                        break;
                    case "1":
                        tc.Text = "Ajax";
                        break;
                    case "2":
                        tc.Text = "数据调用JS";
                        break;
                    case "3":
                        tc.Text = "静态JS";
                        break;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtKeyWord.Text.Length == 0)
            {
                Response.Redirect("UDT_Manage.aspx");
            }
            else
            {
                IsSearch = true;
                BindList(1);
            }
        }

        #region 动态修改链接

        /// <summary>
        ///     动态修改链接
        /// </summary>
        /// <param name="ContentTitle"></param>
        /// <param name="intLong"></param>
        /// <param name="ID"></param>
        /// <param name="Hits"></param>
        /// <returns></returns>
        protected string MyLink(object Name, object ID)
        {
            var winner = new StringBuilder();
            winner.Append(" <a href=\"#\"  onclick=\"");
            winner.Append("return NewTabPage('修改" + Name + "','UDT/UDT_Add.aspx?ID=" + ID + "');\">");
            winner.Append(Name);
            winner.Append("</a>");

            return winner.ToString();
        }

        #endregion
    }
}